package com.ming.controller;

import com.github.pagehelper.PageInfo;
import com.ming.pojo.entity.Dept;
import com.ming.pojo.vo.DeptListVO;
import com.ming.pojo.dto.DeptQueryDTO;
import com.ming.pojo.vo.Result;
import com.ming.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list(DeptQueryDTO deptQueryDTO) {
        log.info("分页查询部门，查询条件: {}", deptQueryDTO);
        PageInfo<DeptListVO> pageInfo = deptService.pageQuery(deptQueryDTO);
        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        deptService.deleteById(id);
        return Result.success();
    }
}